
#include "mainwindow.h"
#include<QPixmap>
#include<QDebug>
#include<QCursor>
#include<stdlib.h>
#include<QTimer>
#include<QCursor>
#include<QMessageBox>
Handler::Handler(QObject *parent) : QObject(parent)
{

}

Handler *Handler::hand=new Handler;

Handler *Handler::getInstance()
{
    return hand;
}

void Handler::addScore()
{
    emit beatMouse();
}
//加分事件
void Handler::subtractScore()
{
    emit beatnoneMouse();
}
//扣分事件
void Handler::setScore()
{
    emit returntozero();
}
//重置分数，归零
void Handler::setTime()
{
    emit resettime();
}
//重置时间
void Handler::stopTime()
{
    emit stoptime();
}
//时间暂停
MyItem::MyItem()
{
    this->setPixmap(QPixmap(":/bg/BeatMouse_P1.jfif"));
    this->start=false;
    this->mouse=false;
}//老鼠洞16个洞

void MyItem::setPic(QString path)
{
    this->setPixmap(QPixmap(path));
}

void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    this->setCursor(QCursor(QPixmap(":/bg/BeatMouse_p3.png")));
    Handler *hand=Handler::getInstance();
    if(this->isStart())
    {
        if(this->isMouse())
        {
            hand->addScore();
        }
        if(!this->isMouse())
        {
            hand->subtractScore();
        }
    }
}//识别鼠标锤子捶打动画

void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    this->setCursor(QCursor(QPixmap(":/bg/BeatMouse_p2.png")));
}//未击打动画

void MyItem::setMouse(bool mouse)
{
    this->mouse=mouse;
}

bool MyItem::isMouse()
{
    return this->mouse;
}

void MyItem::setStart(bool start)
{
    this->start=start;
}

bool MyItem::isStart()
{
    return this->start;
}
MyScene::MyScene(QObject *parent) : QGraphicsScene(parent)
{
    for(int i=0;i<=15;i++)
    {
        this->item[i]=new MyItem;
        this->item[i]->setPos(i/4*180,i%4*180);
        this->addItem(this->item[i]);
    }

    this->ptimer=new QTimer;
    connect(this->ptimer,SIGNAL(timeout()),this,SLOT(showMouse()));
}

void MyScene::showMouse()
{
    //每次开始前重置所有图源
    for(int i=0;i<=15;i++)
    {
        this->item[i]->setPic(":/bg/BeatMouse_P1.jfif");
        this->item[i]->setMouse(false);
    }

    //随机一个数0~15
    //1~2
    int count=rand()%2;
    for(int i=0;i<=count;i++)
    {
        int index=rand()%16;
        this->item[index]->setPic(":/bg/BeatMouse_P9.jpg");
        this->item[index]->setMouse(true);
    }//老鼠随机生成位置
}

void MyScene::startGame()
{
    for(int i=0;i<=15;i++)
    {
        this->item[i]->setStart(true);
        item[i]->setCursor(QCursor(QPixmap(":/bg/BeatMouse_p2.png")));
    }
    //点击开始游戏后，将游戏开始状态设置为 是

    Handler *hand=Handler::getInstance();
    hand->setScore();
    hand->setTime();
    this->ptimer->start(800);
}

void MyScene::endGame()
{
    this->ptimer->stop();
    Handler *hand1=Handler::getInstance();
    hand1->stopTime();

    for(int i=0;i<=15;i++)
    {
        this->item[i]->setStart(false);
    }
    //点击结束游戏后，将游戏开始状态设置为 否

    for(int i=0;i<=15;i++)
    {
        this->item[i]->setPic(":/bg/BeatMouse_P1.jfif");
        this->item[i]->setMouse(false);
    }
    //先暂停游戏，再清空界面
}
